package com.bkitmobile.kesu.appengine.web.client.SupportPopup;

import javax.servlet.http.HttpSession;

import com.bkitmobile.kesu.appengine.web.client.GreetingService;
import com.bkitmobile.kesu.appengine.web.client.GreetingServiceAsync;
import com.bkitmobile.kesu.appengine.web.client.KeSuServer;
import com.bkitmobile.kesu.appengine.web.client.entity.LoginInfo;
import com.bkitmobile.kesu.appengine.web.client.utility.LoginPopupConstant;
import com.bkitmobile.kesu.appengine.web.server.DBConnection;
import com.gargoylesoftware.htmlunit.html.Util;
import com.google.gwt.dev.ModuleTabPanel.Session;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.VerticalSplitPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.core.client.GWT;

public class LoginPopup extends DialogBox{
	private final GreetingServiceAsync greetingService = GWT
		.create(GreetingService.class);

	@SuppressWarnings("deprecation")
	public LoginPopup()
	{
		//super.center();
		//create the form panel, set its action and method
		final FormPanel form = new FormPanel();
		form.setAction("/signin");
		form.setMethod(FormPanel.METHOD_GET);
		//set the main widget for the panel to a vertical panel
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(4);
		form.setWidget(panel);
		
		//create the username field
		//HorizontalPanel usr = new HorizontalPanel();
		//usr.setSpacing(5);
		final Label usr_lbl = new Label();
		usr_lbl.setText(LoginPopupConstant.sign_in);
		
		//usr.add(usr_lbl);
		final TextBox userName_tb = new TextBox();
		userName_tb.setText(LoginPopupConstant.sign_in);
		userName_tb.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				userName_tb.setText("");
			}
		});
		//usr.add(tb);
		panel.add(usr_lbl);
		panel.add(userName_tb);
		
		//create the password field
		final Label pid_lbl = new Label();
		pid_lbl.setText(LoginPopupConstant.pid);
		
		final PasswordTextBox password_tb = new PasswordTextBox();
		password_tb.setText(LoginPopupConstant.pid);
		password_tb.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				password_tb.setText("");
			}
		});
		
		panel.add(pid_lbl);
		panel.add(password_tb);
		
		//create the Submit button
		Button submit_btn = new Button(LoginPopupConstant.submit_btn_txt,new ClickListener() {
			public void onClick(Widget sender) {

				//DBConnection myConn = new DBConnection();
				//myConn.doLogin("", "", true);
				//form.submit();
				final String usrName = userName_tb.getText();
				String pid = password_tb.getText();
				if (userName_tb.getText().length() == 0 || 
						password_tb.getText().length() == 0 ) {
					Window.alert(LoginPopupConstant.warning_text_after_submit);
					return;
				}
				LoginInfo textToServer = new LoginInfo(usrName,pid,true);
				////String textToServer = usrName + ":" + pid + ":true";//cai gi do;
				System.out.print(textToServer);
				greetingService.doLogin(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								
							}

							public void onSuccess(String result) {
								System.out.println("\n Result from server:" + result);
								if (result.equals(LoginPopupConstant.result_success))
								{
									Window.Location.reload();
								}
								if (result.equals(LoginPopupConstant.result_relogin))
								{
									VerticalPanel warning_vertical = new VerticalPanel();
									final PopupPanel warning = new PopupPanel();
									warning.add(warning_vertical);
									warning.setPopupPosition(450, 220);
									warning.setPixelSize(300, 100);
									warning.show();
									Label warning_label = new Label();
									warning_label.setText(LoginPopupConstant.warning_text_relogin);
									warning_vertical.add(warning_label);
									Button warning_button = new Button();
									warning_button.setText(LoginPopupConstant.warning_btn);
									warning_button.addClickHandler(new ClickHandler() 
									{
									
										@Override
										public void onClick(ClickEvent event) {
											// TODO Auto-generated method stub
											warning.hide();
										}
									});
									warning_vertical.add(warning_button);
								}
								if (result.equals(LoginPopupConstant.result_fail))
								{
									
									VerticalPanel warning_vertical = new VerticalPanel();
									final PopupPanel warning = new PopupPanel();
									warning.add(warning_vertical);
									warning.setPopupPosition(450, 220);
									warning.setPixelSize(300, 100);
									warning.show();
									Label warning_label = new Label();
									warning_label.setText(LoginPopupConstant.warning_text_fail);
									warning_vertical.add(warning_label);
									Button warning_button = new Button();
									warning_button.setText(LoginPopupConstant.warning_btn);
									warning_button.addClickHandler(new ClickHandler() 
									{
									
										@Override
										public void onClick(ClickEvent event) {
											// TODO Auto-generated method stub
											warning.hide();
										}
									});
									warning_vertical.add(warning_button);
									
								}
								
							}
						});
				
				form.submit();
			}
		});
		
		//create Cancel button 
		Button cancel_btn = new Button(LoginPopupConstant.cancel_btn_txt,new ClickListener() {
			public void onClick(Widget sender) {
				//form.getParent().setVisible(false);
				//Window.Location.reload();
				form.getParent().setVisible(false);
				//Window.Location.reload();
				hide();
				////set Enable for login_btn and register_btn again
				RootPanel login_area = RootPanel.get("login_btn");
				ToggleButton login_btn = (ToggleButton)login_area.getWidget(0);
				login_btn.setEnabled(true);
				login_btn.setDown(true);
				RootPanel register_area = RootPanel.get("register_btn");
				ToggleButton reg_btn = (ToggleButton)register_area.getWidget(0);
				reg_btn.setEnabled(true);
				reg_btn.setDown(true);
		}});
		
		//Add 2 button into form
		HorizontalPanel horizon = new HorizontalPanel();
		horizon.setSpacing(5);
		horizon.add(submit_btn);
		horizon.add(cancel_btn);
		
		panel.add(horizon);
		
		//add a form handler
		form.addFormHandler(new FormHandler() {
			public void onSubmitComplete(FormSubmitCompleteEvent event) 
			{
				
			}
			public void onSubmit(FormSubmitEvent event) {
			}
		});
		form.setVisible(true);
		
		this.setWidget(form);
		this.setText("Th\u00f4ng tin \u0111\u0103ng nh\u1eadp");
		// Enable animation.
		setAnimationEnabled(true);

		  
		// Enable glass background.
		this.setGlassEnabled(true);
		
		this.setPopupPosition(503, 166);
	}

}
